<template>
  <div class="nav-wrapper">
    <!-- 主导航栏 -->
    <div class="main-nav" :class="{ 'nav-shadow': isScrolled }">
      <el-row justify="space-between" align="middle">
        <el-col :span="6">
          <div class="logo-container">
            <div class="logo-icon-group">
              <el-icon class="logo-icon">
                <Calendar />
              </el-icon>
            </div>
            <span class="site-title">抗日战争史料馆</span>
          </div>
        </el-col>

        <el-col :span="18">
          <el-menu
            :router="true"
            mode="horizontal"
            :ellipsis="false"
            :default-active="activeRoute"
            class="main-menu"
          >
            <el-menu-item
              v-for="route in routes3"
              :key="route.path"
              :index="route.path"
              :class="{ 'is-active': activeRoute === route.path }"
              style="text-align: center"
            >
              <template #title>
                <el-icon>
                  <component :is="route.meta.icon" />
                </el-icon>
                <span>{{ route.meta.title }}</span>
              </template>
            </el-menu-item>
          </el-menu>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from "vue";
import { useRoute } from "vue-router";
import { Calendar } from "@element-plus/icons-vue";
import { routes3 } from "@/router";

const route = useRoute();
const activeRoute = computed(() => route.path);
const isScrolled = ref(false);

const handleScroll = () => {
  isScrolled.value = window.scrollY > 0;
};

onMounted(() => {
  window.addEventListener("scroll", handleScroll);
});

onUnmounted(() => {
  window.removeEventListener("scroll", handleScroll);
});
</script>

<style scoped>
.nav-wrapper {
  position: sticky;
  top: 0;
  z-index: 1000;
  background-color: var(--el-bg-color);
}

.main-nav {
  padding: 0 20px;
  transition: box-shadow 0.3s ease;
}

.nav-shadow {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 60px;
}

.site-title {
  font-size: 20px;
  font-weight: bold;
  color: var(--el-text-color-primary);
}

.main-menu {
  justify-content: flex-end;
}

.el-menu-item.is-active {
  color: var(--el-color-primary);
  background-color: var(--el-color-primary-light-9);
}

.el-menu-item:not(.is-active):hover {
  background-color: var(--el-fill-color-light);
}

@media screen and (max-width: 768px) {
  .site-title {
    font-size: 16px;
  }

  .main-menu {
    justify-content: center;
  }
}
</style>
